package com.qianf.qfoffice.controller;


import com.github.pagehelper.Page;
import com.qianf.qfoffice.entity.ResponseData;
import com.qianf.qfoffice.entity.Skill;
import com.qianf.qfoffice.entity.User;
import com.qianf.qfoffice.service.SkillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SkillController {


    @Autowired
    private SkillService skillService;


    @RequestMapping("/skill/add.do")
    @ResponseBody
    public ResponseData addSkill (String content, HttpSession session) {

        ResponseData<Object> responseData = new ResponseData<>();

        Skill skill = new Skill();

        User user = (User) session.getAttribute("user");

        skill.setUno(user.getNo());
        skill.setContent(content);

        int i = skillService.addSkill(skill);

        if (i > 0 ) {

            responseData.setMsg("添加成功");

        } else {

            responseData.setMsg("添加失败");
            return responseData;
        }

        return responseData;

    }


    @RequestMapping("/skill/querySkill.do")
    @ResponseBody
    public Map<String,Object> findAllSkill (Integer page,Integer limit) {

        HashMap<String, Object> hashMap = new HashMap<>();

        List<Skill> allSkill = skillService.findAllSkill(page, limit);

        long total = ((Page)allSkill).getTotal();

        hashMap.put("code",0);
        hashMap.put("msg","");
        hashMap.put("count",total);
        hashMap.put("data",allSkill);


        return hashMap;
    }

}
